//using System;
//using System.Data;
//using System.Configuration;
//using System.Collections;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Project
{
    public partial class GreetingCardMaker2 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DropDownList lstBackColor;
        protected System.Web.UI.WebControls.DropDownList lstFontName;
        protected System.Web.UI.WebControls.TextBox txtFontSize;
        protected System.Web.UI.WebControls.RadioButtonList lstBorder;
        protected System.Web.UI.WebControls.CheckBox chkPicture;
        protected System.Web.UI.WebControls.TextBox txtGreeting;
        protected System.Web.UI.WebControls.Button cmdUpdate;
        protected System.Web.UI.WebControls.Label lblGreeting;
        protected System.Web.UI.WebControls.Image imgDefault;
        protected System.Web.UI.WebControls.Panel pnlCard;

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            lstBackColor.SelectedIndexChanged += new EventHandler(ControlChanged);
            lstFontName.SelectedIndexChanged += new EventHandler(ControlChanged);
            lstBorder.SelectedIndexChanged += new EventHandler(ControlChanged);
            txtFontSize.TextChanged += new EventHandler(ControlChanged);
            chkPicture.CheckedChanged += new EventHandler(ControlChanged);
            txtGreeting.TextChanged += new EventHandler(ControlChanged);
            this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
            //this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                // Set color options.
                // Get all the values from the KnownColor enumeration.
                string[] colorsArray = Enum.GetNames(typeof(System.Drawing.KnownColor));
                lstBackColor.DataSource = colorsArray;
                lstBackColor.DataBind();
                //lstBackColor.Items.Add("White");
                //lstBackColor.Items.Add("Red");
                //lstBackColor.Items.Add("Green");
                //lstBackColor.Items.Add("Blue");
                //lstBackColor.Items.Add("Yellow");

                // Set font options.
                // Get the list of available fonts and add them to the font list
                System.Drawing.Text.InstalledFontCollection fonts;
                fonts = new System.Drawing.Text.InstalledFontCollection();
                foreach (FontFamily family in fonts.Families)
                {
                    lstFontName.Items.Add(family.Name);
                }
                //lstFontName.Items.Add("Times New Roman");
                //lstFontName.Items.Add("Arial");
                //lstFontName.Items.Add("Verdana");
                //lstFontName.Items.Add("Tahoma");

                // Set border style options by adding a series of
                // ListItem objects.
                ListItem item = new ListItem();

                // The item text indicates the name of the option.
                item.Text = BorderStyle.None.ToString();

                // The item value records the corresponding integer
                // from the enumeration. To obtain this value, you
                // must cast the enumeration value to an integer,
                // and then convert the number to a string so it
                // can be placed in the HTML page.
                item.Value = ((int)BorderStyle.None).ToString();

                // Add the item.
                //lstBorder.Items.Add(item);

                // Now repeat the process for two other border styles.
                item = new ListItem();
                item.Text = BorderStyle.Double.ToString();
                item.Value = ((int)BorderStyle.Double).ToString();
                //lstBorder.Items.Add(item);

                item = new ListItem();
                item.Text = BorderStyle.Solid.ToString();
                item.Value = ((int)BorderStyle.Solid).ToString();
                //lstBorder.Items.Add(item);

                // Select the first border option.
                lstBorder.SelectedIndex = 0;

                // Set Border style option
                string[] borderStylearray = Enum.GetNames(typeof(BorderStyle));
                lstBorder.DataSource = borderStylearray;
                lstBorder.DataBind();


                // Set the picture.
                imgDefault.ImageUrl = "defaultpic.png";
            }
        }

        private void cmdUpdate_Click(object sender, System.EventArgs e)
        {
            UpdateCard(sender, e);
        }

        private void UpdateCard(object sender, System.EventArgs e)
        {
            // Update the color.
            pnlCard.BackColor = Color.FromName(lstBackColor.SelectedItem.Text);

            // Update the font.
            lblGreeting.Font.Name = lstFontName.SelectedItem.Text;
            try
            {
                if (Int32.Parse(txtFontSize.Text) > 0)
                {
                    lblGreeting.Font.Size =
                        FontUnit.Point(Int32.Parse(txtFontSize.Text));
                }
            }
            catch
            {
                // Use error handling to ignore invalid value.
            }

            // Find appropriate TypeConverter for BorderStyle
            TypeConverter cnvrt = TypeDescriptor.GetConverter(typeof(BorderStyle));

            // Update the border style.
            //pnlCard.BorderStyle = (BorderStyle)Int32.Parse(lstBorder.SelectedItem.Value);
            pnlCard.BorderStyle = (BorderStyle) cnvrt.ConvertFromString(lstBorder.SelectedItem.Text);

            // Update the picture.
            if (chkPicture.Checked == true)
            {
                imgDefault.Visible = true;
            }
            else
            {
                imgDefault.Visible = false;
            }

            // Set the text.
            lblGreeting.Text = txtGreeting.Text;
        }

        private void ControlChanged(object sender, System.EventArgs e) { 
            // Refresh because control clicked
            UpdateCard(sender, e);
        }




    }
}
